package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.AdminAgentInfo;
import com.ruoyi.system.domain.AdminPrizeConfig;
import com.ruoyi.system.service.IAdminAgentInfoService;
import com.ruoyi.system.service.IAdminPrizeConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdminAgentRobot;
import com.ruoyi.system.service.IAdminAgentRobotService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理机器人管理Controller
 *
 * @author ruoyi
 * @date 2020-11-19
 */
@RestController
@RequestMapping("/system/adminRobot")
public class AdminAgentRobotController extends BaseController
{
    @Autowired
    private IAdminAgentRobotService adminAgentRobotService;

    @Autowired
    private IAdminAgentInfoService adminAgentInfoService;

    @Autowired
    private IAdminPrizeConfigService iAdminPrizeConfigService;

    /**
     * 查询代理机器人管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:adminRobot:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminAgentRobot adminAgentRobot)
    {
        startPage();
        List<AdminAgentRobot> list = adminAgentRobotService.selectAdminAgentRobotList(adminAgentRobot);
        return getDataTable(list);
    }

    @GetMapping("/getAllRoom")
    public AjaxResult getAllRoom(AdminAgentInfo agentInfo){
        List<AdminAgentInfo> adminAgentInfos = adminAgentInfoService.selectAdminAgentInfoList(agentInfo);
        return AjaxResult.success(adminAgentInfos);
    }

    /**
     * 导出代理机器人管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:adminRobot:export')")
    @Log(title = "代理机器人管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdminAgentRobot adminAgentRobot)
    {
        List<AdminAgentRobot> list = adminAgentRobotService.selectAdminAgentRobotList(adminAgentRobot);
        ExcelUtil<AdminAgentRobot> util = new ExcelUtil<AdminAgentRobot>(AdminAgentRobot.class);
        return util.exportExcel(list, "adminRobot");
    }

    /**
     * 获取代理机器人管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:adminRobot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(adminAgentRobotService.selectAdminAgentRobotById(id));
    }

    /**
     * 新增代理机器人管理
     */
    @PreAuthorize("@ss.hasPermi('system:adminRobot:add')")
    @Log(title = "代理机器人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminAgentRobot adminAgentRobot)
    {
        AdminPrizeConfig maxUpPrice = iAdminPrizeConfigService.selectAdminPrizeConfigByKey("maxUpPrice");
        if (adminAgentRobot.getMaxBet()<adminAgentRobot.getMinBet()) {
            return AjaxResult.error("最大投注数必须大于等于最小投注数");
        }else if(adminAgentRobot.getMinPrice().compareTo(adminAgentRobot.getMaxPrice())>0){
            return AjaxResult.error("最大投注金额必须大于等于最小投注金额");
        }else if(Long.valueOf(maxUpPrice.getConfigValue())>adminAgentRobot.getMaxSum()){
            return AjaxResult.error("最大余额必须大于最大上分金额");
        }
        if(adminAgentRobot!=null&& adminAgentRobot.getFromTo()!=null && adminAgentRobot.getFromTo().length==2 ){
            adminAgentRobot.setBeginTime(adminAgentRobot.getFromTo()[0]);
            adminAgentRobot.setEndTime(adminAgentRobot.getFromTo()[1]);
        }
        return toAjax(adminAgentRobotService.insertAdminAgentRobot(adminAgentRobot));
    }

    /**
     * 修改代理机器人管理
     */
    @PreAuthorize("@ss.hasPermi('system:adminRobot:edit')")
    @Log(title = "代理机器人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminAgentRobot adminAgentRobot)
    {
        AdminPrizeConfig maxUpPrice = iAdminPrizeConfigService.selectAdminPrizeConfigByKey("maxUpPrice");
        if (adminAgentRobot.getMaxBet()<adminAgentRobot.getMinBet()) {
            return AjaxResult.error("最大投注数必须大于等于最小投注数");
        }else if(adminAgentRobot.getMinPrice().compareTo(adminAgentRobot.getMaxPrice())>0){
            return AjaxResult.error("最大投注金额必须大于等于最小投注金额");
        }else if(Long.valueOf(maxUpPrice.getConfigValue())>adminAgentRobot.getMaxSum()){
            return AjaxResult.error("最大余额必须大于最大上分金额");
        }
        if(adminAgentRobot!=null&& adminAgentRobot.getFromTo()!=null && adminAgentRobot.getFromTo().length==2 ){
            adminAgentRobot.setBeginTime(adminAgentRobot.getFromTo()[0]);
            adminAgentRobot.setEndTime(adminAgentRobot.getFromTo()[1]);
        }
        return toAjax(adminAgentRobotService.updateAdminAgentRobot(adminAgentRobot));
    }

    /**
     * 删除代理机器人管理
     */
    @PreAuthorize("@ss.hasPermi('system:adminRobot:remove')")
    @Log(title = "代理机器人管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adminAgentRobotService.deleteAdminAgentRobotByIds(ids));
    }
}
